/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog.base;

import android.annotation.AnimRes;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.utils.LogUtils;

public abstract class StatusBarDialog extends AutoCloseDialog {
    private static final String TAG = "StatusBarDialog";
    private boolean isAnim = true;
    private View mRootView;
    private boolean mReadyDismiss;

    public StatusBarDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void setContentView(int layoutResID) {
        mRootView = LayoutInflater.from(getContext()).inflate(layoutResID, null);
        super.setContentView(mRootView);
    }

    /**
     * 是否开启动画
     *
     * @param anim
     */
    public void setAnim(boolean anim) {
        isAnim = anim;
    }

    long dismissTime = 0l;

    @Override
    public void show() {
        LogUtils.i(TAG, "show() isShow:" + this.isShowing() + " isAnim:" + isAnim + " " + dismissTime);
        //因为 AutoCloseDialog 中 注册了StatusBar的Touch事件 导致Touch后按钮又走按钮的点击事件，目前以下方法零时解决
        if (System.currentTimeMillis() - dismissTime > 100) {
            LogUtils.i(TAG, "show() ok");
            super.show();
            enterAnim();
        } else {
            LogUtils.i(TAG, "show() error " + (System.currentTimeMillis() - dismissTime));
        }
    }

    @Override
    public void dismiss() {
        LogUtils.d(TAG,"-dismiss- mReadyDismiss:"+mReadyDismiss + "--isExitAnimDoing:"+isExitAnimDoing);
        if (mReadyDismiss) {
            doDismiss();
        } else {
            if (!isExitAnimDoing) {
                exitAnim();
            } else {
                doDismiss();
            }
        }
    }


    private void doDismiss() {
        mReadyDismiss = false;
        super.dismiss();
        isExitAnimDoing = false;
    }

    protected void enterAnim() {
        mRootView.startAnimation(getEnterAnim(R.anim.dialog_enter_anim));
    }

    private boolean isExitAnimDoing = false;

    protected void exitAnim() {
        isExitAnimDoing = true;
        Animation animation = getExitAnim(R.anim.dialog_exit_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mReadyDismiss = true;
                isExitAnimDoing = false;
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mRootView.startAnimation(animation);
    }

    private Animation getEnterAnim(@AnimRes int id) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), id);
        return animation;
    }

    private Animation getExitAnim(@AnimRes int id) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), id);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                LogUtils.i(TAG, "getExitAnim dismiss()");
                dismiss();
                cancelTimingClose();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        return animation;
    }
    
    
}
